package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.dao.TreeDao;
import com.example.dao.UserDao;
import com.example.entity.*;

import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class TreeService {

    @Resource
    TreeDao treeDao;

    @Resource
    UserDao userDao;

    public PageInfo<TreeAndTreeName> loadTree(Params params) {
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //接下来的查询会按照当前的分页设置自动进行查询
        List<TreeAndTreeName> list = treeDao.loadTree(params);
        return PageInfo.of(list);
    }

    //用户首页树苗推荐信息
    public List<Tree> findTreeInfo() {
        List<Tree> list = treeDao.findTreeInfo();
        return list;
    }

    //商家在售树苗信息
    public List<Tree> findBuinessTreeInfo(Integer userId) {
        List<Tree> list = treeDao.findBuinessTreeInfo(userId);
        return list;
    }


    public TreeAndTreeName findById(Integer treeId) {
        TreeAndTreeName list = treeDao.findById(treeId);
        return list;
    }

    //通过输入信息检索树苗信息=======================================
    public PageInfo<TreeAndTreeName> findTree(Params params) {
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<TreeAndTreeName> list = treeDao.findTree(params);
        return PageInfo.of(list);
    }

    //通过输入信息筛选出其他商家上架树苗的信息=====================================
    public PageInfo<TreeAndTreeName> findOtherTree(Integer userId, Params params) {
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //拿到当前商家的名称
        String username = userDao.selectById(userId).getUsername();
        List<TreeAndTreeName> list = treeDao.findOtherTree(username, params);
        return PageInfo.of(list);
    }

    //通过树苗类型进行检索树苗信息==========================================
    public PageInfo<TreeAndTreeName> findByTreeType(Params params) {
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<TreeAndTreeName> list = treeDao.findByTreeType(params);
        return PageInfo.of(list);
    }

    public void treeEdit(TreeAndTreeName tree) {
        treeDao.treeEdit(tree);
    }

    public void del(Integer treeId) {
        treeDao.deleteById(treeId);
    }

    //通过树苗年份降序排列获取树苗信息===============================================
    public PageInfo<TreeAndTreeName> findByYearDesc(Params params) {
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //接下来的查询会按照当前的分页设置自动进行查询
        List<TreeAndTreeName> list = treeDao.findByYearDesc(params);
        return PageInfo.of(list);
    }

    //通过树苗年份升序排列获取树苗信息=============================================
    public PageInfo<TreeAndTreeName> findByYearAsc(Params params) {
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //接下来的查询会按照当前的分页设置自动进行查询
        List<TreeAndTreeName> list = treeDao.findByYearAsc(params);
        return PageInfo.of(list);
    }

    //通过树苗价格降序排列获取树苗信息=============================================
    public PageInfo<TreeAndTreeName> findByPriceDesc(Params params) {
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //接下来的查询会按照当前的分页设置自动进行查询
        List<TreeAndTreeName> list = treeDao.findByPriceDesc(params);
        return PageInfo.of(list);
    }

    //通过树苗价格升序排列获取树苗信息
    public PageInfo<TreeAndTreeName> findByPriceAsc(Params params) {
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //接下来的查询会按照当前的分页设置自动进行查询
        List<TreeAndTreeName> list = treeDao.findByPriceAsc(params);
        return PageInfo.of(list);
    }

    //加载系统树苗数量
    public int loadTreeNumber() {
        return treeDao.loadTreeNumber();
    }

    //加载系统树苗种类
    public int loadTypeNumber() {
        return treeDao.loadTypeNumber();
    }


    //树苗添加到收藏
    public void collectTree(User user, Integer treeId) {
        //1. 确定收藏信息中没有该树苗
        //如果通过treeId和username找到的结果为空 说明没有收藏
        //查找信息
        Collect collectinfo = treeDao.findBytreeIdAndUsername(treeId, user.getUsername());
        if (collectinfo == null) {
            //2. 添加收藏
            TreeAndTreeName treeinfo = treeDao.findById(treeId);
            String collectTime = DateUtil.now();
            Collect collect = new Collect(null, treeId, treeinfo.getTreename(), treeinfo.getUsername(), user.getUsername(), treeinfo.getTreetypeid(), collectTime);
            treeDao.collectTree(collect);
        } else {
            throw new CustomException("该树苗已被收藏,请勿重复添加！");
        }


    }

    //加载商家个人中心的收藏信息
    public PageInfo<CollectAndTreeTypeName> findCollect(Params params, Integer userId) {
        User user = userDao.selectById(userId);
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //接下来的查询会按照当前的分页设置自动进行查询
        List<CollectAndTreeTypeName> list = treeDao.findCollect(params, user);
        return PageInfo.of(list);
    }

    public PageInfo<CollectAndTreeTypeName> businessFindCollect(Params params, Integer userId) {
        User userinfo = userDao.selectById(userId);
        //开启分页查询
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        //接下来的查询会按照当前的分页设置自动进行查询
        List<CollectAndTreeTypeName> list = treeDao.businessFindCollect(params, userinfo);
        return PageInfo.of(list);
    }

    //取消收藏
    public void cancelCollect(Integer collectId) {
        treeDao.cancelCollect(collectId);
    }


    //通过商家名称找到树苗信息
    public List<Tree> findTreeByName(String treename,String username) {
        List<Tree> treeinfo = treeDao.findTreeByName(treename,username);
        return treeinfo;
    }


    //加载商家首页报表系统树苗数据
    public List<Integer> getSysData() {
        List<Integer> systemtreelist = new ArrayList<>();
        for (int i = 0; i < 10; i++) systemtreelist.add(0);

        List<Tree> res = treeDao.getSysData();
        for (Tree tree : res) {
            systemtreelist.set(tree.getTreetypeid() - 1, tree.getSellnum());
        }
        return systemtreelist;
    }

    //加载商家首页商家个人数据
    public List<Integer> getUserData(String username) {
        List<Integer> usertreelist = new ArrayList<>();
        for (int i = 0; i < 10; i++) usertreelist.add(0);

        List<Tree> res = treeDao.getUserData(username);
        for (Tree tree : res) {
            usertreelist.set(tree.getTreetypeid() - 1, tree.getSellnum());
        }
        return usertreelist;
    }

    //加载商家首页系统树苗均价数据
    public List<Integer> getAvgPriceData() {
        List<Integer> avgpricelist = new ArrayList<>();
        for (int i = 0; i < 10; i++) avgpricelist.add(0);

        List<Tree> res = treeDao.getAvgPriceData();
        for (Tree tree : res) {
            avgpricelist.set(tree.getTreetypeid() - 1, tree.getTreeprice());
        }
        return avgpricelist;
    }

    //加载商家首页个人树苗均价数据
    public List<Integer> getPersonPriceData(String username) {
        List<Integer> personalpricelist = new ArrayList<>();
        for (int i = 0; i < 10; i++) personalpricelist.add(0);
        List<Tree> res = treeDao.getPersonalPriceData(username);
        for (Tree tree : res) {
            personalpricelist.set(tree.getTreetypeid() - 1, tree.getTreeprice());
        }
        return personalpricelist;
    }


    //商家上架树苗操作
    public void addNewTree(TreeAndTreeName tree, Integer userId) {
        //获取商家信息
        User shopper = userDao.selectById(userId);
        //获取树苗种类id
        int typeId = treeDao.findtypeid(tree.getTreetypename());
        //添加树苗信息到数据库
        Tree treeinfo = new Tree(null,shopper.getUsername(),tree.getTreename(),tree.getTreetime(),tree.getTreeprice(),tree.getTreeproduce(),null,typeId,tree.getSellnum(),null);
        treeDao.addNewTree(treeinfo);

    }

    //商家下架树苗
    public void delTree(Integer treeId) {
        treeDao.delTree(treeId);
    }
}
